package com.alienlab.utils;

//先爬虫的所有的网站，然后下载，下载。（多线程）
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

import com.alibaba.fastjson.JSONArray;
import com.googlecode.asyn4j.core.callback.AsynCallBack;
import com.googlecode.asyn4j.core.handler.CacheAsynWorkHandler;
import com.googlecode.asyn4j.core.handler.DefaultErrorAsynWorkHandler;
import com.googlecode.asyn4j.core.handler.FileAsynServiceHandler;
import com.googlecode.asyn4j.service.AsynService;
import com.googlecode.asyn4j.service.AsynServiceImpl;



public class NewsManage {
private static AsynService asynService = null;
	private Properties propertyConfig = new PropertyConfig("jdbc.properties").getProperties();
	private String news_url=  propertyConfig.getProperty("url");
	private String dummy_path=  propertyConfig.getProperty("dummy-path");
	/**
	 * 异步调用机制初始化，在系统刚启动时初始化systemstart。
	 */
	public static void initAnyscDownloader() {
		asynService = AsynServiceImpl.getService(300, 3000L, 100, 100, 1000);
		// 异步工作缓冲处理器
		asynService.setWorkQueueFullHandler(new CacheAsynWorkHandler(100));
		// 服务启动和关闭处理器
		asynService.setServiceHandler(new FileAsynServiceHandler());
		// 异步工作执行异常处理器
		asynService.setErrorAsynWorkHandler(new DefaultErrorAsynWorkHandler());
		// 启动服务
		asynService.init();
	}
	//给异步调用加任务
	/**
	 * url 地址组
	 */
	public static boolean addTask(NewsBeanData newsBeanData,AsynCallBack callback) {
			asynService.addWork(NewsProcess.class, "getResult" ,new Object[] {newsBeanData},callback);
			return true;		
	}
	
	/**
	 * url 地址,
	 */
	public static boolean addTaskload(String url ,String i,long time_,AsynCallBack callback) {
		asynService.addWork(NewsLoadProcess.class, "getResult" ,new Object[] {url,i,time_},callback);
		return true;
	}
		
	/**
	 * 执行下载并且获取到文件下载地址
	 * @return jsonarry类型
	 */
	public List<NewsBeanData> getUrlAndDownLoad(List<String> name,List<String> url,String getpath){
		System.out.println(dummy_path);
		System.out.println(news_url);
		long start=System.currentTimeMillis();
		JSONArray jsonArray = new JSONArray();
		List<NewsBeanData> list = new ArrayList<>();
		for(int i  = 0 ; i < name.size()  ;i++){
			NewsBeanData newsBeanData = new NewsBeanData(name.get(i), url.get(i),jsonArray,getpath);
			newsBeanData.getData();
			list.add(newsBeanData);
		}
		for(int i  = 0 ; i < list.size() ; i++){
			if("1".equals(list.get(i).getJsonArray().getString(0))){
				i=-1;
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				continue;
			}
		}
		long end=System.currentTimeMillis();
		return list;		
	}
	
	public JSONArray getLoadPdf(String url){
		String today = NowTime.getNowTime("yyyy-MM-dd"); 
		long end=System.currentTimeMillis();
		NewsJsoup newsJsoup = new NewsJsoup();
		String getpath = ""+dummy_path+"";
		getpath = getpath.replace("\\", "\\\\");
		JSONArray jsonArray = new JSONArray();
		JSONArray jsonArray_ = new JSONArray();
		try {
			 jsonArray_ = newsJsoup.getNeedFile_(url, "999", getpath);
		} catch (IOException e) {
			System.out.println("网址错误");
		}
		int jsonlen = jsonArray_.size();
		for(int i = 0 ; i < jsonlen ; i ++){
			jsonArray.add(""+news_url+"+"+today+"/"+today+"-"+end+"-"+i+".pdf");
			NewsManage.addTaskload(jsonArray_.getString(i), String.valueOf(i), end, new NewsUrlCallback());
		}
		return jsonArray;
	}

	class NewsUrlCallback extends AsynCallBack{

		@Override
		public void doNotify() {

		}
	}
}
